<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>时间差</title>
</head>

<body>
    <script>
        //1.准备两个时间节点
        var time1 = new Date(2021, 1, 23, 11, 22, 18)
        var time2 = new Date(2021, 1, 25, 3, 45, 36)

        //准备一个函数，接受两个参数
        function getDiff(time1, time2) {
            //2.获取到两个时间节点的时间戳
            var ms1 = time1.getTime()
            var ms2 = time2.getTime()
            console.log(ms1)
            console.log(ms2)

            //3.两个时间节点的时间戳得到相差的毫秒数
            var sub1 = ms2 - ms1
            console.log(sub1)
            //3.两个时间节点的时间戳得到相差的秒数
            var sub = Math.ceil((ms2 - ms1) / 1000)
            console.log(sub)

            //4.开始换算
            //4-1 换算天
            var day = Math.floor(sub / (60 * 60 * 24))

            //4-2 换算小时
            var hours = Math.floor(sub % (60 * 60 * 24) / (60 * 60))//sub%(60*60*24)得到的不够一天的秒数 除以一小时的秒数向下取整

            //4-3 换算分钟
            var minutes = Math.floor(sub % (60 * 60) / 60)//sub%(60*60)得到的是不够一小时的秒数 除以一分钟的秒数向下取整

            //4-3 换算秒钟
            var seconds = Math.floor(sub % 60)
            return { day: day, hours: hours, minutes: minutes, seconds: seconds }
        }
        var res = getDiff(time1, time2)
        console.log(res)
    </script>
</body>

</html>